﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Admin_User_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
		this.Form.Action = "Backend/User/Default.aspx";
		if (!IsPostBack)
		{
			GetAllUsers();
		}
    }

	// Display all users
	protected void GetAllUsers()
	{
		UserList.DataSource = Membership.GetAllUsers();
		UserList.DataBind();
	}
	// Delele user
	protected void UserList_RowDeleting(object sender, GridViewDeleteEventArgs e)
	{
		string username = this.UserList.Rows[e.RowIndex].Cells[0].Text;
		try
		{
			Membership.DeleteUser(username);
			displayMessage.Text = string.Format("{0} User was deleted!", username);
		}
		catch (Exception err)
		{
			Utilities.LogError(err);
			throw;
		}
		finally
		{
			GetAllUsers();
		}
	}

	// Make row editable
	protected void UserList_RowEditing(object sender, GridViewEditEventArgs e)
	{
		UserList.EditIndex = e.NewEditIndex;
		GetAllUsers();
	}

	// Update user
	protected void UserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		int index = UserList.EditIndex;
		GridViewRow gvRow = this.UserList.Rows[e.RowIndex];
		string username = this.UserList.Rows[e.RowIndex].Cells[0].Text;
		string email = ((TextBox)gvRow.Cells[1].Controls[0]).Text;
		MembershipUser user = Membership.GetUser(username);
		try
		{
			user.Email = email;
			Membership.UpdateUser(user);
		}
		catch (Exception err)
		{
			Utilities.LogError(err);
		}
		finally
		{
			UserList.EditIndex = -1;
			GetAllUsers();
		}
	}
	protected void UserList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		UserList.EditIndex = -1;
		GetAllUsers();
	}
}